<?php

namespace App\Controllers;

use App\Models\UserModel;
use App\Requests\UserForgetLoginPasswordRequest;
use App\Requests\UserUpdateNicknameRequest;
use App\Requests\UserRegisterRequest;
use App\Requests\UserLoginRequest;
use Framework\BaseController;
use Framework\Hope;
use Framework\ModelEncryptionTrait;
use App\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Str;


class UserController extends BaseController
{
//    use ModelEncryptionTrait;

    private $UserService;

    public function __construct(UserService $UserService)
    {
        $this->UserService = $UserService;
    }

    /**用户注册
     * @param UserRegisterRequest $request
     * @throws \Throwable
     */
    public function register(UserRegisterRequest $request)
    {
        $phone = $request->phone();
        $code = $request->code();
        $password = $request->password();
        $list = $this->UserService->register($phone, $code, $password);
        $this->retSuccess($list);
    }


    public function getUserInfo()
    {
        $user_id = $this->getAuthId();
        $list = $this->UserService->getUserInfo($user_id);
        $this->retSuccess($list);
    }

    public function getTaskList()
    {
        $user_id = $this->getAuthId();
        $list = $this->UserService->getTaskList($user_id);
        $this->retSuccess($list);
    }

    /** 忘记登录密码
     * @param UserForgetLoginPasswordRequest $request
     * @throws \Throwable
     */
    public function forgetLoginPassword(UserForgetLoginPasswordRequest $request)
    {

        $phone = $request->phone();
        $password = $request->password();
        $code = $request->code();
        $res =  $this->UserService->forgetLoginPassword($phone,$code, $password);
        $this->retSuccess($res);
    }

    /**  修改用户昵称
     * @param Request $request
     */
    public function editNickname(Request $request)
    {
        $nickname = $request->input('nickname');

        $user_id = $this->getAuthId();
        $this->UserService->editNickname($user_id, $nickname);
        $this->retSuccess(true);
    }


    /** 修改头像
     * @param Request $request
     */
    public function editAvatar(Request $request)
    {
        $user_id = $this->getAuthId();
        $path = $request->input('avatar');
        if(!$path){
            $this->retError("头像不能为空");
        }
        $user = UserModel::query()->where('id',$user_id)->update(['avatar'=>$path]);
        $this->retSuccess($user);
    }



}
